<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-11-15
 * Time: 17:46
 */

namespace App\Services\CrawlerService\driver;


use App\Services\CrawlerService\HtmlService;
use App\Services\LbsService\LbsService;
use DB;

class Xinxin21ccnnDriver extends HtmlService
{
    protected $prefix       = "http://www.21ccnn.com/";
    protected $list_url     = 'http://www.21ccnn.com/plugin.php?id=aljzp&act=list&rid=1&view=table&page={page}';
    protected $list_page    = "page";
    protected $list_regular = "DIV.wp DIV.wp DIV.container DIV.grid DIV.section DIV.section UL.sep LI.table-view-item DIV.clearfix SPAN.table-view-body A";
    protected $list_range   = 1;

    protected $detail_rules = [
        "title"       => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.viewad H2.viewad-title"],
        "phone"       => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.maincol DIV.media DIV.media-body SPAN.pull-left"],
        "long_detail" => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.maincol DIV.viewad-descript DIV.typo-p SPAN.long", 'multi' => true],
        "detail"      => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.maincol DIV.viewad-descript DIV.typo-p SPAN.normal", 'multi' => true],
        "content"     => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.maincol DIV.maincol DIV.tab-content DIV"],
        "posted_at"   => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.maincol DIV.viewad-meta SPAN SPAN", 'attr' => 'title'],
        "comments"    => ['node' => "DIV.wp DIV DIV.wp DIV.wrapper DIV.maincol DIV UL.comments LI.comment", 'multi' => true],
    ];

    protected $detail_keys = [
        "职位类别" => "position",
        "薪资水平" => "money",
        "招聘人数" => "people",
        "类别"   => "type",
        "工作时间" => "work_time",
        "性别要求" => "gender",
        "学历要求" => "edu",
        "联系人"  => "contacts",
        "公司名称" => "company",
        "地区"   => "address",
    ];

    protected $detail_comments = [];

    public function getAllMd5()
    {
        return DB::table('crawlers')->get(['md5'])->pluck('md5')->toArray();
    }

    public function renderFieldLongDetail($node, $rule)
    {
        $text = $node->innertext;
        $this->renderKeyValue($text);
    }

    public function renderFieldDetail($node, $rule)
    {
        $text = $node->innertext;
        $this->renderKeyValue($text);
    }

    public function renderFieldContent($node, $rule)
    {
        $text = preg_replace("/该信息来至.*/", "", $node->innertext);
        $this->autoFilter($text);
        return $text;
    }

    public function renderFieldComments($node)
    {
        $text = $node->innertext;
        $this->autoFilter($text);
        preg_match("/(.*?)\s+(\d{4}\-\d{2}\-\d{2}\s+\d{2}\:+\d{2}\:+\d{2})\s+(.*)/", $text, $m);
        if ($m) {
            $this->autoFilter($m[2]);
            $this->autoFilter($m[3]);
            return [
                "created_at" => $m[2],
                "content"    => $m[3],
            ];
        }
        return [];
    }

    public function renderKeyValue($text)
    {
        $this->autoFilter($text);
        list($key, $value) = explode('：', $text);
        if (isset($this->detail_keys[$key])) {
            if ($this->detail_keys[$key] == 'money') {
                $value = str_replace('元', '', $value);
                if (str_contains($value, '-')) {
                    list($min, $max) = explode('-', $value);
                    $this->autoFilter($min);
                    $this->autoFilter($max);
                    $this->detail_data['money_min'] = trim($min);
                    $this->detail_data['money_max'] = trim($max);
                } else {
                    $this->autoFilter($value);
                    if ($value == "面议") {
                        $this->detail_data['money_min'] = 0;
                        $this->detail_data['money_max'] = 0;
                    } else {
                        $this->detail_data['money_min'] = trim($value);
                        $this->detail_data['money_max'] = trim($value);
                    }
                }
            } elseif ($this->detail_keys[$key] == 'address') {
                preg_match('/(.*?\-){3}(.*)/', $value, $m);
                $address = trim(str_replace("[地图及交通]", '', $m[2]));
                $this->autoFilter($address);
                $this->detail_data['address'] = $address;
                $lbs=new LbsService();
                $geo=$lbs->address("广东省云浮市新兴县".$address);
                $this->detail_data['geo'] = $geo->toGeo();
                list($lat,$lng)=$geo->toPoint();
                $this->detail_data['lat'] = $lat;
                $this->detail_data['lng'] = $lng;
            } elseif ($this->detail_keys[$key] == 'work_time') {
                $this->autoFilter($value);
                switch ($value) {
                    case "做五休二":
                        $this->detail_data['holiday'] = round(2 / 7, 2);
                        break;
                    case "做六休一":
                        $this->detail_data['holiday'] = round(1 / 7, 2);
                        break;
                    default:
                        $this->detail_data['holiday'] = 0;
                        break;
                }
                $this->detail_data['work_time'] = $value;
            } else {
                $this->autoFilter($value);
                $this->detail_data[$this->detail_keys[$key]] = $value;
            }
        }
    }

    protected function afterCrawl(&$data)
    {
        $this->detail_comments = $data['comments'];
        unset($data['detail'], $data['long_detail'], $data['comments']);
    }

    public function save()
    {
        $id = DB::table('positions')->insertGetId($this->detail_data);
        foreach ($this->detail_comments as &$value) {
            $value['position_id'] = $id;
        }
        DB::table('comments')->insert($this->detail_comments);
        return $id;
    }
}